

from selenium import webdriver
import time
from lxml import etree
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import base64
import requests
from yun import base64_api
from PIL import Image

driver = webdriver.Chrome()

driver.get('http://www.ttshitu.com/login.html')

driver.find_element_by_name('userName').send_keys('logic')
driver.find_element_by_name('password').send_keys('logic123')

# data:image/jpg;base64     编码方式
image_url = driver.find_element_by_id('captchaImg').get_attribute('src')
# print(type(image_url))
# image_data = image_url.split(',')[1]
# image = base64.b64decode(image_data)
# while True:
#     if "captchaImg" in driver.find_element_by_id('captchaImg').get_attribute('id'):
image = requests.get('http://api.ttshitu.com/captcha_v2').json()
# print(image['img'])

image_data = base64.b64decode(image['img'])

with open('captcha1.png', 'wb') as f:
    f.write(image_data)

img_path = "captcha1.png"
img = Image.open(img_path)
result = base64_api(uname='logic', pwd='logic123', img=img)

driver.find_element_by_name('captcha').send_keys(result)
time.sleep(2)
driver.find_element_by_class_name('layui-btn').click()
    # else:
    #     break

# 验证码  服务器给你的 保存  数据库  随机的验证码 redis xxxxx  {xxxx:1111}

# web项目
# 账号
# 密码
# 验证码





